<import type="js" file="common/sortable/jqueryui189" />
<import type="js" file="common/sortable/jqueryuisortablemin" />
<div>
    <div style="float:left"><input type="file" id="upload[id]" name="upload[id]" />
    <input type="hidden" id="[id]" name="[id]" value="[value]" /></div> <div id="msg_paixu" style="float:left;padding-left:20px;padding-top:10px;color:#555;"></div><div class="c"></div>
    <div id="div[id]" class="piclist" style=" margin:10px 0px;">
    </div>
    <div class="c"></div>
</div>
<script type="text/javascript">
	var value = "[value]";
	if(value == "")
	{
		$("#div[id]").hide();
	}
	$('#upload[id]').uploadify({
			'swf': '__PUBLIC__/common/uploadify/uploadify.swf")',
			'uploader': "{:U('Fwadmin/Login/uploadFile')}?path=[path]&sessionid={:session_id()}",
			'cancelImg': '__PUBLIC__/common/uploadify/uploadify-cancel.png',
			'buttonText':'选择文件',
			'auto'     : true,
			'height': 24,
			'width': 80,
     		'multi': false,
			'fileTypeDesc': '文件',
			'fileTypeExts':'*.rar;*.zip;*.doc;*.xls;*.ppt;*.docx;*.xlsx;*.pptx;*.pdf;*.jpg;*.gif;*.png;*.bmp;*.txt;',
			'fileSizeLimit':'2024KB',
			'removeTimeout':0,
			'onUploadError': function (file, errorCode, errorMsg, errorString, swfuploadifyQueue) {
				if(errorCode !== "-280"&&errorMsg !== "File Upload Cancelled.")
				{
					alert(errorMsg);
				}
			},
		'onCancel':function(event,queueID,file,data){
		   //  alert("取消成功！");
		},
		'onUploadComplete':function (file) { //每个文件上传完成
			
		},
		'onUploadSuccess': function (file, data, response) {
		　//　　alert(file+'-'+data+'--'+response);
			if (response == true) {
				var json = eval('('+data+')');
				if(json.SaveName != undefined)
				{
				   $("#[id]").val($("#[id]").val()+json.SaveName+',');
				   showMorePic($("#[id]").val());
				}
			}
			else {
				alert("上传出错！");
			}
		}
	});
	
	//uploadify上传多文件删除文件
	function delfile2(filename,i) {
		if(filename!=='')
		{
		   $.ajax({
			  url:"{:U('Fwadmin/Index/delPic')}?filename="+filename,
			  success:function(val)
			  {
				  var old_val = $("#[id]").val();
				  var new_val = old_val.replace(filename+',','');
				  $("#[id]").val(new_val);
				  $("#files"+i).hide();
			  }
		  });
		  var filename2 = filename.replace('[path]','[path]/thumb');
		  $.ajax({
			  url:"{:U('Fwadmin/Index/delPic')}?filename="+filename2,
			  success:function(val)
			  {
			  }
		  })
		}
	}

	//添加多文件
	function addfile(filename,i) {
		var file = $("#filename").val();
		var ca_id = $("#ca_id").val();
		if(file==""){
			alert('请填写文件名！')
			return false;
		}
		$.ajax({
			url:"{:U('Fwadmin/ZlCa/addFile')}",
			type: "POST",
			data:{fileurl:filename,filename:file,ca_id:ca_id},
			success:function(val)
			{
				if(val=="success"){
					alert('添加成功！');
					window.location.reload()
					$("#files"+i).hide();
				}else{
					alert('添加失败！');
				}
				
			}
		});
		
	}

	function showMorePic(str)　{
			var html = '';
			if(str !== '')
			{
				str = str.substring(0,str.length-1);
				//str = str.TrimEnd(',');
				var files = str.split(',');
				for(var i=0;i<files.length;i++)
				{
					if(files[i] !== '')
					{
						html += '<div id="files'+[i]+'"><form action="" method="post"> <input name="fileurl" id="fileurl"  value="'+files[i]+'" style="width:350px" > 文件名：<input type="text" name="filename" id="filename"  style="width:350px" ><input type="hidden" id="ca_id" name="ca_id" value="{$model.ca_id}"/><br/><a href="__ROOT__'+files[i]+'" target="_blank">'+files[i]+'</a><input type="button" onclick="addfile(\''+files[i]+'\','+i+')" value="添加"><input type="button"  onclick="delfile2(\''+files[i]+'\','+i+')" value="删除"/></form></div>';
					}
				}
				$("#div[id]").html(html);
				$("#div[id]").show();
			} else {
				$("#div[id]").html(html);
				$("#div[id]").hide();
			}	
	} 
	
	
</script>